package com.zjuee.web.basic.captcha.utils;


import com.zjuee.base.bean.response.CommonResultCode;
import com.zjuee.base.exception.BaseException;

import java.awt.*;
import java.net.URL;

/**
 * @author wu liang
 * @since 1.0
 */
public class FontUtil {

    /**
     * 定义默认字体
     */
    private static Font font_ch;
    private static Font font_en;
    public static Font getDefaultFont_CH() {
        if (font_ch == null) {
            URL resource = CaptchaUtil.class.getClassLoader().getResource("captcha/fonts/simsun.ttc");
            if (resource == null)
                throw new BaseException(CommonResultCode.USER_LOGIN_EXP, "中文字体获取失败");
            try {
                font_ch = Font.createFont(Font.TRUETYPE_FONT, resource.openStream());
            } catch (Exception e) {
                throw new BaseException(CommonResultCode.USER_LOGIN_EXP, "中文字体获取失败");
            }
        }
        return font_ch;
    }
    public static Font getDefaultFont_EN() {
        if (font_en == null) {
            URL resource = CaptchaUtil.class.getClassLoader().getResource("captcha/fonts/arial.ttf");
            if (resource == null)
                throw new BaseException(CommonResultCode.USER_LOGIN_EXP, "英文字体获取失败");
            try {
                font_en = Font.createFont(Font.TRUETYPE_FONT, resource.openStream());
            } catch (Exception e) {
                throw new BaseException(CommonResultCode.USER_LOGIN_EXP, "英文字体获取失败");
            }
        }
        return font_en;
    }

    public static Font getFont_CH(int style, float size) {
        Font defaultFont = getDefaultFont_CH();
        return defaultFont.deriveFont(style, size);
    }
    public static Font getFont_EN(int style, float size) {
        Font defaultFont = getDefaultFont_EN();
        return defaultFont.deriveFont(style, size);
    }
}
